import {
  Body,
  Controller,
  Get,
  Post,
  Query,
  ParseIntPipe,
  UsePipes,
  ValidationPipe
} from '@nestjs/common'
import { PostsService } from './posts.service'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { CreatePostDto } from './dto/create-posts-dto'

@ApiTags('文章')
@Controller('posts')
export class PostsController {
  constructor(readonly posts: PostsService) {}

  @ApiOperation({ summary: '新增' })
  @Post('create')
  // 开启自动转换body参数
  @UsePipes(new ValidationPipe({ transform: true }))
  create(@Body() req: CreatePostDto, @Body('type', ParseIntPipe) type) {
    return this.posts.create({ ...req, type })
  }

  @ApiOperation({ summary: '删除' })
  @Post('deleteById')
  deleteById(@Body() req) {
    return this.posts.deleteById(req)
  }

  @Post('deleteByIds')
  deleteByIds(@Body() req) {
    return this.posts.deleteByIds(req)
  }

  @ApiOperation({ summary: '更新' })
  @Post('update')
  update(@Body() req) {
    return this.posts.update(req)
  }

  @ApiOperation({ summary: '查询所有文章' })
  @Post('findAll')
  findAll(@Body() req) {
    return this.posts.findAll(req)
  }

  @ApiOperation({ summary: '查询文章详情' })
  @Get('getPostsById')
  getPostsById(@Query() req) {
    return this.posts.getPostsById(req)
  }
}
